package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.dto.integral.IntegralDto;
import com.java110.dto.integral.PreStoreUserIntegralDto;
import com.java110.intf.acct.IIntegralV1InnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("sendIntegralToUserBmoImpl")
public class SendIntegralToUserBmoImpl implements IMallCommonApiBmo {

    @Autowired
    private IIntegralV1InnerServiceSMO integralV1InnerServiceSMOImpl;

    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "link", "未包含手机号");
        Assert.hasKeyAndValue(reqJson, "integral", "未包含赠送积分数");
        Assert.hasKeyAndValue(reqJson, "orderId", "未包含订单ID");
    }

    @Override
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {

        IntegralDto integralDto = new IntegralDto();
        integralDto.setLink(reqJson.getString("link"));
        integralDto.setObjType(IntegralDto.OBJ_TYPE_PERSON);
        List<IntegralDto> integralDtos = integralV1InnerServiceSMOImpl.queryIntegrals(integralDto);

        if (ListUtil.isNull(integralDtos)) {
            throw new IllegalArgumentException("积分账户不存在");
        }

        PreStoreUserIntegralDto preStoreUserIntegralDto = new PreStoreUserIntegralDto();

        preStoreUserIntegralDto.setIntegralId(integralDtos.get(0).getIntegralId());
        preStoreUserIntegralDto.setAmount(reqJson.getDoubleValue("integral"));
        preStoreUserIntegralDto.setOrderId(reqJson.getString("orderId"));
        preStoreUserIntegralDto.setRemark(reqJson.getString("remark"));
        integralV1InnerServiceSMOImpl.preStoreUserIntegral(preStoreUserIntegralDto);
    }
}
